/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-07-16 13:58
 */
package com.yao.sys.service.impl;

import com.yao.sys.entity.dto.MenuDto;
import com.yao.sys.mapper.MenuMapper;
import com.yao.sys.service.AuthService;
import com.yao.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户权限实现类
 * @author yaoyuming
 * @date 2020/07/16 13:58
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<MenuDto> listMenuTree(Integer userId) {

        // 检查用户
        userService.checkUser(userId);

        // 菜单列表
        List<MenuDto> menuList = menuMapper.listMenuTreeByUser(userId);

        List<MenuDto> tree = menuList.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());

        tree.stream().forEach(e -> {
            List<MenuDto> child = menuList.stream().filter(menu -> menu.getParentId() == e.getId()).collect(Collectors.toList());
            e.setChild(child);
        });

        return tree;

    }

}